#include "contactwidget.h"
#include "ui_contactwidget.h"

//5月11日 完成查询，发消息跳转
//联系人列表的追加与联系人缓冲区的追加信号绑定，只要向联系人缓冲区
ContactWidget::ContactWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ContactWidget)
{
    ui->setupUi(this);
    menu=new QMenu(this);
    count=0;
    if(FriendApplyLib::getInstance()->isEmpty())
    {
        ThreadManger::getInstance()->loadFriendApply();
    }
    ui->widget->setParent(this);
    ui->widget->setUseroc(FriendListLib::getInstance()->getUserOC());
    QAction *deleteaction=menu->addAction("删除");
    connect(deleteaction,&QAction::triggered,this,&ContactWidget::deleteContact);
    rightHide();
    connect(ContactLib::getInstance(),&ContactLib::s_friendLibLoadDone,this,&ContactWidget::loadFriendToUI);
    connect(ContactLib::getInstance(),&ContactLib::s_appendFriend,this,&ContactWidget::addContact);
    ThreadManger::getInstance()->loadContactListDB();
}



ContactWidget::~ContactWidget()
{
    if(count==1)
        ThreadManger::getInstance()->removeFriendOne(removeFriendoc);
    else if(count>1)
        ThreadManger::getInstance()->removeFriendList(removeFriendList);
    else
    {}

    delete ui;
}


//添加联系人到联系人列表
void ContactWidget::addContact(const QString oc,const QString name)
{
    QListWidgetItem *item=new QListWidgetItem("");
    ui->listWidget->addItem(item);
    ContactBox *contactbox=new ContactBox(ui->listWidget);
    qDebug()<<AvatarLib::getInstance()->getAvatar(oc);
    contactbox->setAvatar(AvatarLib::getInstance()->getAvatar(oc));
    contactbox->setText(name);
    contactbox->setOC(oc);
    ui->listWidget->setItemWidget(item,contactbox);
    item->setSizeHint(QSize(ui->listWidget->width(),84));
    contactlist.push_back(contactbox);
}


//搜索OC及Name
void ContactWidget::searchOCName(QString str)
{
    QSet<QString> resultoc;
    contactlist.clear();
    ui->listWidget->clear();
    auto ocit = oclist.begin();
    auto nameit = namelist.begin();
    for (; ocit != oclist.end()&&nameit != namelist.end(); ocit++,nameit++) {
        QString ockey = ocit.key(); // 获取键
        QString namekey=nameit.key();
        if(ockey.contains(str)){
            resultoc.insert(ockey);
        }
        else if(!resultoc.contains(nametooc[namekey])&&namekey.contains(str)){
            resultoc.insert(nametooc[namekey]);
        }
        else
            continue;
    }
    for(QString i:resultoc)
        addContact(i,octoname[i]);
}

//右部区域可见
void ContactWidget::rightShow()
{
    ui->label_contactname->show();
    ui->label_contactavatar->show();
    ui->label_oc->show();
    ui->btn_chat->show();
    ui->btn_else->show();
    ui->btn_videocall->show();
    ui->btn_voicecall->show();
    rightIsEmpty=false;
}

//右部区域隐藏
void ContactWidget::rightHide()
{
    ui->label_contactname->hide();
    ui->label_contactavatar->hide();
    ui->label_oc->hide();
    ui->btn_chat->hide();
    ui->btn_else->hide();
    ui->btn_videocall->hide();
    ui->btn_voicecall->hide();
    rightIsEmpty=true;
}

void ContactWidget::appendContact(const QString &friendoc, const QString &friendname)
{
    ContactLib::getInstance()->appendContact(friendoc,friendname);
}


//从contactlib中加载联系人信息到界面
void ContactWidget::loadFriendToUI()
{
    ui->listWidget->clear();
    oclist.clear();
    namelist.clear();
    octoname.clear();
    nametooc.clear();
    contactlist.clear();
    QList<QPair<QString,QString>> tmp;
    tmp=ContactLib::getInstance()->getFriendList();
    int count=0;
    for(QPair<QString,QString> i:tmp)
    {
        addContact(i.first,i.second);
        oclist.insert(i.first,count);
        namelist.insert(i.second,count);
        octoname.insert(i.first,i.second);
        nametooc.insert(i.second,i.first);
        count++;
    }
    if(ui->listWidget->item(0))
    {
        rightShow();
        ui->label_contactavatar->setPixmap(contactlist[0]->getAvatar());
        ui->label_contactname->setText(contactlist[0]->getName());
        ui->label_oc->setText(QString("OC号: %1").arg(contactlist[0]->getOC()));
        ui->listWidget->setCurrentRow(0);
        currentName=contactlist[0]->getName();
        currentOC=contactlist[0]->getOC();

    }
}



//联系人列表一项点击
void ContactWidget::on_listWidget_itemClicked(QListWidgetItem *item)
{
    Q_UNUSED(item);
    if(rightIsEmpty)
        rightShow();
    int currentrow=ui->listWidget->currentRow();
    ContactBox* currentbox=contactlist[currentrow];
    ui->label_contactavatar->setPixmap(currentbox->getAvatar());
    ui->label_contactname->setText(currentbox->getName());
    ui->label_oc->setText(QString("OC号: %1").arg(currentbox->getOC()));
    currentName=contactlist[currentrow]->getName();
    currentOC=contactlist[currentrow]->getOC();
    currentIndex=currentrow;
}

//点击聊天按钮，跳转
void ContactWidget::on_btn_chat_clicked()
{
    if(ui->label_contactname->text().isEmpty())return;
    PageManger::switchToChat(currentOC);
}

//搜索框文字变动，实时搜索
void ContactWidget::on_lineEdit_search_textEdited(const QString &arg1)
{

    rightHide();
    searchOCName(arg1);
}

void ContactWidget::on_btn_else_clicked()
{
    QPoint globalPos = ui->btn_else->mapToGlobal(QPoint(0, ui->btn_else->height()));
    menu->exec(globalPos);
}

//删除联系人
void ContactWidget::deleteContact()
{
    contactlist.removeAt(currentIndex);
    oclist.remove(currentOC);
    namelist.remove(currentName);
    octoname.remove(currentOC);
    nametooc.remove(currentName);
    delete  ui->listWidget->takeItem(currentIndex);
    rightHide();
    ui->listWidget->clearSelection();
    //删除数据库中联系人表数据
    ThreadManger::getInstance()->deleteContactDB(currentOC);
    //删除缓冲区oc
    ContactLib::getInstance()->removeContact(currentOC);
    //删除消息列表的oc
    FriendListLib::getInstance()->removeFriend(currentOC);
    //删除好友的头像
    ThreadManger::getInstance()->deleteAvatar(currentOC);
    //删除数据库中聊天记录表
    ThreadManger::getInstance()->dropChatHistroyTable(currentOC);
    if(count==0)
    {
        removeFriendoc=currentOC;
    }
    removeFriendList.push_back(currentOC);
    PageManger::appendDelFriendoc(currentOC);
    count++;
    currentOC=QString();
    currentName=QString();
    currentIndex=-1;
}

void ContactWidget::on_btn_addfriend_clicked()
{
    addfriendwidget=new AddFriendWidget();
    addfriendwidget->setModal(true); // 设置为模态对话框
    addfriendwidget->show();
    addfriendwidget->exec(); // 使用exec()方法使窗口变为模态，阻塞父窗口
}
